home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap4 / 4_6 / dyn_c / dynhtm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  1.9 KB  |  115 lines

  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "cgilib.h"
  6.  
  7. void main(int argc, char *argv[])
  8. {
  9.     /* Variables for the data. */
  10.     
  11.     char *data = (char *) 0;
  12.     Dictionary dataDict = 0;
  13.     const char *choice = 0, *title = 0;
  14.     FILE *in = 0;
  15.     char buffer[1024];
  16.     int actual = 0;
  17.     int i;
  18.     
  19.     /* Write the content type */
  20.     
  21.     printf("Content-type: text/html\n\n");
  22.     
  23.     /*
  24.     * Create a dictionary to hold the data.
  25.     */
  26.     
  27.     dataDict = dict_alloc();
  28.     
  29.     /*
  30.      * Read the data, passing the string data by reference.
  31.      * readData() will alloc space for it. readData() will also determine
  32.      * the request type.
  33.      */
  34.     
  35.     readData(&data);
  36.         
  37.     /*
  38.      * Call parseData() to break the data into key-value pairs.
  39.      * This function will print the pairs.
  40.      */
  41.     if(data) parseData(data,dataDict);
  42.  
  43.     choice = (const char *)dict_valueForKey(dataDict,"choice");
  44.     
  45.     if(choice && !strcmp(choice,"library"))
  46.       {
  47.         in = fopen("cgilib.c","r");
  48.         title = "CGI input library";
  49.       }
  50.     else
  51.       {
  52.         in = fopen("dynhtm.c","r");
  53.         title = "dynhtm.pl script";
  54.       }
  55.       
  56.     /* output the HTML header */
  57.     printf("<HTML>\n");
  58.     printf("<HEAD>\n");
  59.     
  60.     
  61.     printf("<TITLE>%s</TITLE>\n",title);
  62.     
  63.     
  64.     printf("</HEAD>\n");
  65.     printf("<BODY>\n");
  66.     printf("<PRE>\n");
  67.  
  68.     while(in && !feof(in))
  69.     {
  70.         actual = fread(buffer,sizeof(char),1024,in);
  71.         
  72.         /* Output the file, converting special characters along the way. */
  73.         i = 0;
  74.         while(buffer[i] && (i<actual))
  75.         {
  76.             if(buffer[i] == '\"')
  77.             {
  78.                 printf(""");
  79.             }
  80.             else if(buffer[i] == '&')
  81.             {
  82.                 printf("&");
  83.             }
  84.             else if(buffer[i] == '>')
  85.             {
  86.                 printf(">");
  87.             }
  88.             else if(buffer[i] == '<')
  89.             {
  90.                 printf("<");
  91.             }
  92.             else
  93.             {
  94.                 putc(buffer[i],stdout);
  95.             }
  96.             
  97.             i++;
  98.         }
  99.     }
  100.  
  101.     /* Output the html footer */
  102.     printf("</PRE>\n");
  103.     printf("</BODY>\n");
  104.     printf("</HTML>\n");
  105.  
  106.     fclose(in);
  107.     if(data) free(data);
  108.     
  109.     dict_free(dataDict);
  110.     
  111.     /* End the program */
  112.     exit(0);
  113. }
  114.  
  115.